/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.mobbuzz.renderer;

import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Font;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.List;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.ListCellRenderer;
import edu.mobbuzz.bean.Message;
import edu.mobbuzz.bean.RSSChannel;
import edu.mobbuzz.bean.Category;
import edu.mobbuzz.facade.CategoryFacade;
import edu.mobbuzz.facade.LabelFacade;
import edu.mobbuzz.facade.RSSChannelFacade;
import edu.mobbuzz.util.theme.Theme;
import java.io.IOException;
import java.util.Vector;

public class MessageRenderer extends Container implements ListCellRenderer {

        private Label titleLbl = new Label("");
        private Label descLbl = new Label("");
        private Label labelLbl = new Label("");
        private Label pic = new Label("");
        private Image icon = null;
        private Label focus = new Label("");
        private LabelFacade labelFacade;
        private CategoryFacade categoryFacade;
        private RSSChannelFacade rssChannelFacade;
        
        public MessageRenderer() {
            setLayout(new BorderLayout());
            addComponent(BorderLayout.WEST, pic);
            Container cnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
           
            titleLbl.getStyle().setFont(Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
            titleLbl.getStyle().setBgTransparency(0);  
            titleLbl.getStyle().setMargin(0, 0, 0, 0);
            
            descLbl.getStyle().setFont(Theme.getFontDescriptionStyle("SansSerif"));
            descLbl.getStyle().setFgColor(0xFFFFFF);
            descLbl.getStyle().setBgTransparency(0);
            descLbl.getStyle().setMargin(0, 0, 0, 0);

            labelLbl.getStyle().setFont(Theme.getFontDescriptionStyle("SansSerif"));
            labelLbl.getStyle().setFgColor(0xF59FBC);
            labelLbl.getStyle().setBgTransparency(0);
            labelLbl.getStyle().setMargin(0, 0, 0, 0);
            
            pic.getStyle().setBgTransparency(0);
            
            cnt.addComponent(titleLbl);
            cnt.addComponent(descLbl);
            cnt.addComponent(labelLbl);
            
            addComponent(BorderLayout.CENTER, cnt);
            focus.getStyle().setBgTransparency(100);
            focus.getStyle().setBgColor(0xCCCCCC);
        }

        public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
            String type="";
            Message message = (Message) value;
            StringBuffer labelStr = new StringBuffer();

            Vector rssChannelArr = getRssChannelFacade().getIdBySource(message.getSource());
            for(int i=0;i<rssChannelArr.size();i++){
                RSSChannel rssChannel = (RSSChannel)rssChannelArr.elementAt(i);
                Vector labelArr = getLabelFacade().getLabelByRss(rssChannel.getRecId());
                for(int j=0;j<labelArr.size();j++){
                    edu.mobbuzz.bean.Label lbl = (edu.mobbuzz.bean.Label)labelArr.elementAt(j);
                    Category category = getCategoryFacade().getCategoryById(lbl.getRecIdCategory());
                    if(i>0)
                        labelStr.append(" - ");
                    labelStr.append(category.getLabel());
                }
            }

            
            titleLbl.setText(message.getFrom());
            descLbl.setText(message.getDescription());
            labelLbl.setText(labelStr.toString());
            
            if(message.isRead())
                type="/icons/read.gif";
            else
                type="/icons/unread.gif";
            
            try {
                icon = Image.createImage(type);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            pic.setIcon(icon);
            return this;
        
        }

        public Component getListFocusComponent(List list) {
            return focus;
        }

        public LabelFacade getLabelFacade() {
            if(labelFacade==null)
                labelFacade = new LabelFacade();
            return labelFacade;
        }

        public CategoryFacade getCategoryFacade() {
            if(categoryFacade==null)
                categoryFacade = new CategoryFacade();
            return categoryFacade;
        }
        
        public RSSChannelFacade getRssChannelFacade() {
            if(rssChannelFacade==null)
                rssChannelFacade = new RSSChannelFacade();
            return rssChannelFacade;
        }
    }
